package com.tqw.chd.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tqw.chd.mapper.ItemDescMapper;
import com.tqw.chd.mapper.ItemMapper;
import com.tqw.chd.pojo.Item;
import com.tqw.chd.pojo.ItemDesc;
import com.tqw.chd.vo.ItemVO;
import com.tqw.chd.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Alex
 */
@Service
public class ItemServiceImpl implements ItemService{

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    @Autowired
    private FileService fileService;

    /**
     * 拉取商品并分类
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getItemList(PageResult pageResult) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        //动态sql
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "title",pageResult.getQuery());
        //使用MP的page对象
        Page<Item> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        Page<Item> itemPage = itemMapper.selectPage(page, queryWrapper);

        long total = itemPage.getTotal();
        List<Item> records = itemPage.getRecords();
        return pageResult.setTotal(total).setRows(records);

    }

    /**
     * 修改商品状态
     * @param item
     */
    @Override
    @Transactional
    public void updateItemStatus(Item item) {
        itemMapper.updateById(item);
    }

    /**
     * 删除商品及其图片
     * @param id
     */
    @Override
    @Transactional
    public void deleteItemById(Integer id) {
        //根据id 查询item表的数据
        Item item = itemMapper.selectById(id);
        String images = item.getFontImage();
        //删除单张图片
        fileService.deleteFile(images);

        //删除详情中的所有图片
        ItemDesc itemDesc = itemDescMapper.selectById(id);
        String[] descImages =itemDesc.getImages().split(",");
        for (String path:descImages){
            fileService.deleteFile(path);
        }

        //删除商品数据
        itemMapper.deleteById(id);
        itemDescMapper.deleteById(id);
    }

    /**
     * 根据id查找商品
     * @param id
     * @return
     */
    @Override
    public Item findItemById(Integer id) {
        return itemMapper.selectById(id);
    }

    /**
     * 修改商品信息
     * @param item
     */
    @Override
    @Transactional
    public void updateItemById(Item item) {
        itemMapper.updateById(item);
    }

    /**
     * 新增商品
     * @param itemVO
     */
    @Override
    @Transactional
    public void savaItem(ItemVO itemVO) {
        //入库商品表
        Item item = itemVO.getItem();
        item.setStatus(true);
        itemMapper.insert(item);
        //商品入库后回显出id
        ItemDesc itemDesc = itemVO.getItemDesc();
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);
    }


}
